# To change this template, choose Tools | Templates
# and open the template in the editor.
from game.gui.game_area import *
from game.gui.container import *
from game.gui.label import *
from game.gui.widget import *

from game.state.moving import MovingState
from game.pathing import PathManager
from pygame.sprite import Group
from game.object.character import OrderedDict
from game.level_generator import LevelGenerator
import game.fov as fov
import pygame
import random

from pygame.locals import *
from game.resource_manager import *
#from pgu import gui
#from pgu import html

from game.object.object import *
from game.object.character import *

from game.schedule_manager import ScheduleManager
from game.ai.action_manager import ActionManager
from game.state.state_manager import StateManager
from game.gui_manager import GuiManager

from game.state.wait import WaitState
from game.state.open import OpenState
from game.state.close import CloseState

class GameData:
    def __init__(self):

        self.sprite_pitch = (32, 32)

        pygame.display.set_caption('Rogue')
        pygame.key.set_repeat(250, 50)

        self.gui = GuiManager(self)
        self.res_mgr = ResourceManager(self.sprite_pitch)
        self.gui.icons = self.res_mgr.load_gui_icons(16)
        self.path_mgr = PathManager()

        lvl_gen = LevelGenerator(self.res_mgr)
        self.level = lvl_gen.generate_level('dungeon', '1')
        self.res_mgr.load_tileset(self.level)

        self.spr_rect = (self.sprite_pitch, self.sprite_pitch)


        self.main_char = self.res_mgr.create_char("guy")
        self.gui.equ_mgr.set(self.main_char.equipment)
        self.gui.equ_mgr.rebuild()

        self.gui.inv_mgr.set(self.main_char.inventory)
        self.gui.inv_mgr.rebuild()

        self.level.random_tile(occupant='none').set_occupant(self.main_char)

        self.char_sprites = OffsetGroup(self.main_char)

        self.sched_mgr = ScheduleManager()
        self.sched_mgr.add_char(self.main_char, 0)

        self.en_list = ["gobo_shoe", "kool_kat", "octav"]
        max_en = len(self.en_list) - 1

        n_en = random.randint(5,15)
        for i in xrange(0, n_en):
            en_t = random.randint(0, max_en)
            en = self.res_mgr.create_npchar(self.en_list[en_t])
            self.level.random_tile(occupant='none').set_occupant(en)
            self.char_sprites.add(en)
            self.sched_mgr.add_char(en, 1)

        items = []
        self.itm_list = OrderedDict([
            ("weapon", [
                "Cutlass", "Zweihander",
                "Double Axe"
                ]),
            ("armor", [
                "Steel Plate Mail", "Iron Plate Mail",
                "Silver Plate Mail", "Bronze Plate Mail",
                "Titanium Plate Mail", "Aluminium Plate Mail",
                "Tin Plate Mail"
                ])
        ])
        max_itm = len(self.itm_list.items()) - 1

        n_itm = random.randint(6,18)
        for i in xrange(0, n_itm):
            itm_t = random.randint(0, max_itm)
            itm_d = self.itm_list.items()[itm_t]
            itm_slist = itm_d[1]
            max_itm_s = len(itm_slist) - 1
            itm_st = random.randint(0, max_itm_s)
            itm_sd = itm_slist[itm_st]
            itm = self.res_mgr.create_item(itm_sd, itm_d[0])
            self.level.random_tile().add_item(itm)
            items.append(itm)


        self.item_sprites = StackGroup(items)

        self.gui.game_area.adjust_view()
        fov.do_fov(self.level, self.main_char.pos(), 10)

        back = self.res_mgr.load_block(self.gui.game_area.game_rect.size, (64,64,64))
        self.level.create_background(back)

        self.tile_vis = self.res_mgr.load_block(self.sprite_pitch, (255,255,255), 64)
        self.tile_invis = self.res_mgr.load_block(self.sprite_pitch, (0, 0, 0), 255)
        self.tile_fogvis = self.res_mgr.load_block(self.sprite_pitch, (0, 0, 0), 192)

        self.state_mgr = StateManager()
        self.state_mgr.states = {
            "wait":WaitState(self),
            "open":OpenState(self),
            "close":CloseState(self),
            "moving":MovingState(self)
        }
        self.state_mgr.push_state("wait")

        self.act_mgr = ActionManager()

        self.hilite = Cursor('hilite')
        self.hilite.image = self.res_mgr.load_block(self.sprite_pitch, (64, 255, 64), 64)

class OffsetGroup(pygame.sprite.Group):
    def __init__(self, spr):
        pygame.sprite.Group.__init__(self, spr)

    def draw(self, surface, offset):
        sprites = self.sprites()
        surface_blit = surface.blit
        for spr in sprites:
            self.spritedict[spr] = surface_blit(spr.image, (spr.rect[0] - offset[0], spr.rect[1] - offset[1]))
        self.lostsprites = []

class StackGroup(OffsetGroup):
    def __init__(self, spr):
        OffsetGroup.__init__(self, spr)

    def draw(self, surface, offset):
        sprites = self.sprites()
        surface_blit = surface.blit
        drawn = []
        for spr in sprites:
            spr_pos = "{0}_{1}".format(spr.rect[0], spr.rect[1])
            if spr_pos in drawn:
                #draw a icon for indicating multiple items on this tile
                pass
            else:
                self.spritedict[spr] = surface_blit(spr.image, (spr.rect[0] - offset[0], spr.rect[1] - offset[1]))
                drawn.append(spr_pos)
        self.lostsprites = []